package com.py.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.py.model.common.dtos.PageResponseResult;
import com.py.model.common.dtos.ResponseResult;
import com.py.model.common.enums.AppHttpCodeEnum;
import com.py.model.wemedia.dtos.SensitiveDto;
import com.py.model.wemedia.pojos.WmSensitive;
import com.py.wemedia.mapper.WmSensitiveMapper;
import com.py.wemedia.service.WmSensitiveService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        //参数判断
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive sensitive = getById(id);
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "要删除的敏感词不存在");
        }
        //  删除
        boolean flag = removeById(sensitive);

        return ResponseResult.okResult(flag);
    }

    /**
     * 列表展示
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listBySensitive(SensitiveDto dto) {
        //参数校验
        dto.checkParam();
        //添加分页条件
        IPage page = new Page(dto.getPage(), dto.getSize());
        //构造查询参数
        LambdaQueryWrapper<WmSensitive> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(dto.getName())) {
            lqw.like(WmSensitive::getSensitives, dto.getName());
        }
        lqw.orderByDesc(WmSensitive::getCreatedTime);
        page(page, lqw);

        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());

        result.setData(page.getRecords());

        return result;
    }

    /**
     * 新增敏感词
     *
     * @param sensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive sensitive) {
        //检查参数
        if (StringUtils.isEmpty(sensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词为空");
        }
        LambdaQueryWrapper<WmSensitive> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WmSensitive::getSensitives, sensitive.getSensitives());
        WmSensitive one = getOne(lqw);
        //判断敏感词是否存在
        if (one != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词已存在");
        }
        sensitive.setCreatedTime(new Date());
        save(sensitive);
        return ResponseResult.okResult("ok");
    }

    /**
     * 修改敏感词
     *
     * @param sensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive sensitive) {
        //参数判断
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "请输入敏感词");
        }
        sensitive.setCreatedTime(new Date());
        updateById(sensitive);


        return ResponseResult.okResult("ok");
    }
}
